<template>
  <div class="app-container">

    <!-- 条件查询 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="出版社名称:">
        <el-input v-model.trim="query.name" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
        <el-button icon="el-icon-refresh"  @click="reload">重置</el-button>
        <el-button icon="el-icon-circle-plus-outline" type="primary" @click="openAdd" >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="list"
      stripe
      border
      style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
      <el-table-column align="center" prop="id" label="出版社编号" width="120"></el-table-column>
      <el-table-column align="center" prop="name" label="出版社名称"></el-table-column>
      <el-table-column  align="center" label="操作" width="330">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row.id)" size="mini">编辑</el-button>
          <el-button type="danger"  @click="handleDelete(scope.row.id)"  size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 50]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>

    <!-- 编辑组件 -->
    <edit :title="edit.title" :formData="edit.formData" :visible="edit.visible" :remoteClose="remoteClose" />

  </div>
</template>

<script>

import * as api from '@/api/press'

import Edit from './edit'

export default {
  name: 'Press', // 和对应路由表中配置的name值一致

  components: {Edit},

  data() {
    return {
      list: [],
      page: {
        current: 1,
        size: 20,
        total: 0
      },

      query: {},

      edit: {
        title: '',
        visible: false,
        formData: {}
      }
    }
  },

  created() {
    this.fetchData()
  },

  methods: {
    async fetchData() {
      const {data} = await api.search(this.query, this.page.current, this.page.size)
      this.list = data.rows
      this.page.total = data.total
    },

    // val 是切换之后的每页显示多少条
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },

    // 当页码改变后触发到此方法，val 是当前点击（或输入）的那个页码，
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    },

    // 条件查询
    queryData() {
      // 将页码变为1，第1页
      this.page.current = 1
      this.fetchData()
    },

    // 重置
    reload() {
      this.query = {}
      this.fetchData()
    },

    // 编辑
    handleEdit(id) {
      api.getById(id).then(response => {
        if(response.code === 20000) {
          this.edit.formData = response.data
          this.edit.title  = '编辑'
          this.edit.visible = true
        }
      })
    },

    // 删除
    handleDelete(id) {
      this.$confirm('确认删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送删除请求
        api.deleteById(id).then(response => {
          // 处理响应结果提示
          this.$message({
            type: response.code === 20000 ? 'success': 'error',
            message: response.message
          })
        })
        // 刷新列表数据
        this.fetchData()
      }).catch(() => {
        // 取消删除，不用理会
      })
    },



    // 弹出新增窗口
    openAdd() {
      this.edit.title = "新增出版社"
      this.edit.visible = true
    },

    // 关闭弹窗
    remoteClose() {
      this.edit.formData = {}
      this.edit.visible = false
      this.fetchData()
    }
  }
}
</script>
